package com.tza.web.t_net.client.sceens;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.layout.VLayout;
import com.tza.web.t_net.client.ActionContext;
import com.tza.web.t_net.client.screenservices.DataProviderService;
import com.tza.web.t_net.client.screenservices.DataProviderServiceAsync;
import com.tza.web.t_net.shared.entity.DataMap;

public class SimpleGrid extends Composite
{

   ActionContext                          actionContext;

   private final DataProviderServiceAsync sampleService = GWT.create(DataProviderService.class);
   private VLayout                        layout        = new VLayout(30);

   final ListGrid                         simpleGridComp;
   ListGridField[]                        fields;

   public SimpleGrid(ActionContext mainActionContext, int Width, int Height, ListGridField[] gridFields)
   {
      // Initialisation du widget
      initWidget(layout);

      simpleGridComp = new ListGrid();
      // simpleGridComp.setWidth(Width);
      // simpleGridComp.setHeight(Height);
      simpleGridComp.setShowAllRecords(true);
      fields = gridFields;

      simpleGridComp.setDefaultFields(fields);
      simpleGridComp.setCanResizeFields(true);
      // Ajout du formulaire
      layout.addMember(simpleGridComp);

      layout.draw();

   }

   public void LoadData(String service, DataMap criteria)
   {
      final ListGridRecord record = new ListGridRecord();
      sampleService.getGridData(service, criteria, new AsyncCallback<List<DataMap>>()
      {

         public void onFailure(Throwable caught)
         {
            Window.alert("RPC to Load Grid Data");
         }

         public void onSuccess(List<DataMap> result)
         {

            for (DataMap dataMap : result)
            {
               for (ListGridField gridField : fields)

                  record.setAttribute(gridField.getName(), dataMap.get(gridField.getName()));
               simpleGridComp.addData(record);

            }
         }

      });

   }

   public void changeToVisible()
   {
      this.setVisible(true);
   }

}
